package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品三级分类
 *
 * @author ShiJieLin
 * @email ShiJieLin@gmail.com
 * @date 2022-03-29 09:39:34
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /** 返回商品的树形三级分类
     * @return
     */
    @RequestMapping("/list/tree")
    public R listTree(){

        List<CategoryEntity> categoryEntityList = categoryService.listTree();

        return R.ok().put("data", categoryEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /** 保存当前拖拽修改成功之后的顺序
     * @param category
     * @return
     */
    @RequestMapping("update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateDetail(category);

        return R.ok();
    }

    /** 删除指定ID的节点
     * @param catIds
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){

        // 检查当前被删除的菜单节点是否在其他的地方有引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));


        return R.ok();
    }

}
